import { $ } from "../modules/public.js";
import ajax from "../modules/ajax.js";
import toast from "../modules/toast.js";

$(".tel").value = sessionStorage.getItem("tel") || "";

$(".btn").onclick = function(){
  ajax({
    url:"http://localhost:3000/login",
    data:{
      tel:$(".tel").value,
      password: $(".pw").value
    }
  }).then(res=>{
    res = JSON.parse(res);
    console.log(res);
    if(res.code === 0){
      toast({
        type:"error",
        title:"该手机号未注册",
        close:()=>{
          $(".tel").value = "";
          $(".tel").focus();
        }
      })
    }else if(res.code === 2){
      toast({
        type:"error",
        title:"密码错误",
        close:()=>{
          $(".pw").value = "";
          $(".pw").focus();
        }
      })
    }else{
      toast({
        type:"success",
        title:"登录成功",
        close:()=>{
          sessionStorage.setItem("token", res.data);
          location.assign("./index.html");
        }
      })
    }
  })
}

